package com.yd.obtainhashsign

import android.content.Intent
import android.os.AsyncTask
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.toolbar
import kotlinx.android.synthetic.main.activity_select_package.*


class SelectPackageActivity : AppCompatActivity() {

    lateinit var adapter: BaseQuickAdapter<AppSigningUtil.AppInfo, BaseViewHolder>
    val packageResult = mutableListOf<AppSigningUtil.AppInfo>()
    val filterResult = mutableListOf<AppSigningUtil.AppInfo>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_select_package)
        setSupportActionBar(toolbar)
        toolbar.title = "Select Package"
        initView()
        getPackage()
    }

    val watcher = object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

        override fun afterTextChanged(s: Editable) {
            filterList(s.toString().trim())
        }

    }

    private fun filterList(filterText: String) {
        filterResult.clear()
        if (filterText.isEmpty()) {
            filterResult.addAll(packageResult)
        } else {
            val filterList = packageResult.filter { info ->
                info.appName.contains(filterText, true) ||
                        info.packageName.contains(filterText, true)
            }
            filterResult.addAll(filterList)
        }
        adapter.setList(filterResult)
    }

    private fun initView() {
        adapter = object :
            BaseQuickAdapter<AppSigningUtil.AppInfo, BaseViewHolder>(R.layout.activity_select_package_item) {
            override fun convert(holder: BaseViewHolder, item: AppSigningUtil.AppInfo) {
                holder.setImageDrawable(R.id.ivIcon, item.appIcon)
                holder.setText(R.id.tvAppName, item.appName)
                holder.setText(R.id.tvPackageName, item.packageName)
            }
        }
        adapter.setOnItemClickListener { _, view, position ->
            val data = adapter.getItem(position)
            val intent = Intent()
            intent.putExtra(MainActivity.ResultKey, data.packageName)
            setResult(MainActivity.Requst_Code, intent)
            finish()
        }
        rvPackages.adapter = adapter
        etFilter.addTextChangedListener(watcher)
    }

    private fun getPackage() {
        val task = object : AsyncTask<Int, Int, List<AppSigningUtil.AppInfo>>() {
            override fun doInBackground(vararg params: Int?): List<AppSigningUtil.AppInfo> {
                return AppSigningUtil.getPackages(this@SelectPackageActivity)
            }

            override fun onPostExecute(result: List<AppSigningUtil.AppInfo>) {
                progress_circular.visibility = View.GONE
                packageResult.addAll(result)

                filterList("")

            }
        }
        task.execute(0)
    }

    override fun onDestroy() {
        etFilter.removeTextChangedListener(watcher)
        super.onDestroy()
    }


}
